package com.geese.module.admin.controller;

import java.util.Map;

import com.geese.common.entity.Business;
import com.geese.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geese.common.BaseController;
import com.geese.common.constants.Constants;
import com.geese.common.entity.Courier;
import com.geese.common.util.BeanUtil;
import com.geese.common.util.TextValidator;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.module.admin.vo.CeoQuery;
import com.geese.module.admin.vo.CeoVO;
import com.geese.module.ceo.service.CourierService;
import com.geese.module.user.service.UserService;
import com.google.common.collect.Maps;

/**
 * @author YeFangYi 
 */
@Controller
@RequestMapping(value = "/admin")
@Scope("prototype")
public class CeoController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private CourierService courierService;


    /**
     * 新增CEO
     */
    @PostMapping("/ceo/save")
    @ResponseBody
    public String ceoSave(@RequestBody CeoVO ceoVO, @RequestParam String token){
    	if(StringUtils.isEmpty(ceoVO.getCode())) {
    		throw new BusinessException(120011);
    	}
    	if(StringUtils.isEmpty(ceoVO.getMobile()) || !TextValidator.isMobileExact(ceoVO.getMobile())) {
    		throw new BusinessException(120012);
    	}
    	if(StringUtils.isEmpty(ceoVO.getName())) {
    		throw new BusinessException(120013);
    	}
    	if(!StringUtils.isNumeric(ceoVO.getSchoolId())) {
    		throw new BusinessException(120014);
    	}
    	userService.checkUser(token);
    	
    	Courier courier = BeanUtil.map(ceoVO, Courier.class);
    	courierService.saveCeo(courier);
        return response();
    }

    /**
     * 修改CEO
     */
    @PostMapping("/ceo/update")
    @ResponseBody
    public String ceoUpdate(@RequestBody CeoVO ceoVO, @RequestParam String token){
    	if(!StringUtils.isNumeric(ceoVO.getId())) {
    		throw new BusinessException(120010);
    	}
    	if(StringUtils.isEmpty(ceoVO.getCode())) {
    		throw new BusinessException(120011);
    	}
    	if(StringUtils.isEmpty(ceoVO.getMobile()) || !TextValidator.isMobileExact(ceoVO.getMobile())) {
    		throw new BusinessException(120012);
    	}
    	if(StringUtils.isEmpty(ceoVO.getName())) {
    		throw new BusinessException(120013);
    	}
    	userService.checkUser(token);
    	
    	Courier courier = BeanUtil.map(ceoVO, Courier.class);
    	courierService.updateCeo(courier);
        return response();
    }
    
    /**
     * 删除CEO
     */
    @PostMapping("/ceo/remove")
    @ResponseBody
    public String ceoRemove(@RequestParam String id, @RequestParam String token){
    	if(!StringUtils.isNumeric(id)) {
    		throw new BusinessException(120010);
    	}
    	userService.checkUser(token);
    	courierService.removeCeo(Integer.valueOf(id));
        return response();
    }
    
    /**
     * 查询CEO
     */
    @GetMapping("/ceo/listInPage")
    @ResponseBody
    public String schoolList(CeoQuery query) {
    	if(StringUtils.isNotEmpty(query.getSchoolId()) && !StringUtils.isNumeric(query.getSchoolId())) {
    		throw new BusinessException(120015);
    	}
    	userService.checkUser(query.getToken());
    	Page page = getPageInfo(query);
		Map<String, Object> result = courierService.listCeoByQuery(query, page);
        return response(result);
    }
}
